<template>
  <div class="tab-bar-item" @click='itemClick'>

    <div v-if='!isActive'>
      <slot name='item-icon'></slot>
    </div>

    <div v-else>
      <slot name='item-icon-active'></slot>
    </div>

    <div :style="activeStyle">
      <slot name='item-text'></slot>
    </div>

  </div>
</template>

<script>
  export default {
    name: '',
    props: {
      path: String,
      activeColor: {
        type: String,
        default: 'red'
      }
    },
    data() {
      return {
      }
    },
    computed: {
      /**
       *  判断当前的TabBarItem是否是被点击的活跃状态的
       *  根据自己保存的path和活跃路由的path=>$route.path对比
       *  找出活跃的路由,然后动态决定isActive的值
       *  return this.path === this.$route.path
       */
      isActive() {
        return this.$route.path.indexOf(this.path) !== -1
      },

      /**
       *  动态绑定style,用一个计算属性返回结果,如果当前的TabBarItem活跃,则返回{***}  
       *  如果当前的TabBarItem不活跃,则返回{}
       */
      activeStyle() {
        return this.isActive ? { color: this.activeColor } : {}
      }
    },
    methods: {

      /**
       *  点击跳转路由 跳转的路由由外界传递给props中的path
       *  跳转之前判断是否 依然在本 页面内 ,在本页面内不跳转
       *  要不然报错 
       */
      itemClick() {
        if (this.path == this.$route.path) {
          return
        }
        this.$router.replace(this.path)
      }
    },
    components: {

    }
  }
</script>

<style scoped>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }

  .tab-bar-item img {
    margin-top: 3px;
    width: 24px;
    height: 24px;
    vertical-align: middle;
    margin-bottom: 2px;
  }

  .active {
    color: red;
  }
</style>